package com.iniapp.app.jsf;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.inibb.app.exception.ServiceVLDException;

/**
 * Simple JSF Utility methods.
 */
public class JSFUtility {

	private static Logger logger = LoggerFactory.getLogger(JSFUtility.class);

	/**
	 * Getting current faces context
	 * 
	 * @return current context
	 */
	// @Provides @FaceContext @Named
	public FacesContext getCurrentContext() {
		return FacesContext.getCurrentInstance();
	}

	public static FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	/**
	 * Gets current http session
	 * 
	 * @return current http session
	 */
	/*public static HttpSession getCurrentSession() {
		HttpSession session = (HttpSession) getFacesContext().getExternalContext().getSession(false);
		return session;
	}*/

	private static void addMessage(String summary, String detail, Severity severity) {
		FacesMessage facesMessage = new FacesMessage(severity, summary, detail);
		getFacesContext().addMessage(null, facesMessage);
	}

	public static void addServiceVLDException(ServiceVLDException vlde) {
		/*
		 * for(Entry<String, AppMessage> entry: vlde.getErrors().entrySet()){
		 * addError(entry.getKey(), entry.getValue().getMsgKey(),
		 * entry.getValue().getParams()); }
		 */
	}

	public static String getRequestParameter(String name) {
		FacesContext fc = FacesContext.getCurrentInstance();
		String param = fc.getExternalContext().getRequestParameterMap().get(name);
		if (param != null && param.trim().length() > 0) {
			return param;
		} else {
			return null;
		}
	}
	/**
     * Creates and adds error message.
     * 
     * @param code code
     */
    public static void addError(/*String code, */String defaultMessage, Object...params){
//    	addMessageCode(code, defaultMessage, FacesMessage.SEVERITY_ERROR, params);
    	addMessage(null, defaultMessage, FacesMessage.SEVERITY_ERROR);
    }
    
    /**
     * Creates and adds warn message.
     * 
     * @param code code
     */
    public static void addWarn(/*String code,*/ String defaultMessage, Object...params){
//    	addMessageCode(code, defaultMessage, FacesMessage.SEVERITY_WARN, params);
    	addMessage(null, defaultMessage, FacesMessage.SEVERITY_WARN);
    }
    
    /**
     * Creates and adds info message.
     * 
     * @param code code
     */
    public static void addInfo(/*String code, */String defaultMessage, Object...params){
//    	addMessageCode(code, defaultMessage, FacesMessage.SEVERITY_INFO, params);
    	addMessage(null, defaultMessage, FacesMessage.SEVERITY_INFO);
    }

}
